<template>
	<view class="lucky">
		<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
		<image class="name" src="../../../../static/image/lucky-title.png" mode="widthFix"></image>
		<view class="content">
			<view :class="['animate',{'active':active}]"></view>
		</view>
		<image class="bottom" src="../../../../static/image/lucky-bg.png" mode="widthFix"></image>
	</view>
</template>

<script>
	let x = 20;
	let y = 20;
	export default {
		data() {
			return {
				active: false,
				ctx: '', //绘图图像
				points: [] //路径点集合 
			}
		},
		created() {
			this.ctx = uni.createCanvasContext("mycanvas", this); //创建绘图对象

			//设置画笔样式
			this.ctx.lineWidth = 10;
			this.ctx.setStrokeStyle('#d34bff');
			this.ctx.lineCap = "round"
			this.ctx.lineJoin = "round"
		},
		methods: {
			//触摸开始，获取到起点
			touchstart: function(e) {
				this.active = true;
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};
				this.points.push(startPoint);
				//每次触摸开始，开启新的路径
				this.ctx.beginPath();
				this.$emit('gestureStart');
			},

			//触摸移动，获取到路径点
			touchmove: function(e) {
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint); //存点
				let len = this.points.length;
				if (len >= 2) {
					this.draw(); //绘制路径
				}

			},

			// 触摸结束，将未绘制的点清空防止对后续路径产生干扰
			touchend: function() {
				this.points = [];
				this.clear();
				this.$emit('gestureEnd');
			},

			/* ***********************************************
			#   绘制笔迹
			#	1.为保证笔迹实时显示，必须在移动的同时绘制笔迹
			#	2.为保证笔迹连续，每次从路径集合中区两个点作为起点（moveTo）和终点(lineTo)
			#	3.将上一次的终点作为下一次绘制的起点（即清除第一个点）
			************************************************ */
			draw: function() {
				let point1 = this.points[0]
				let point2 = this.points[1]
				this.points.shift()
				this.ctx.moveTo(point1.X, point1.Y)
				this.ctx.lineTo(point2.X, point2.Y)
				this.ctx.stroke()
				this.ctx.draw(true)
			},

			//清空画布
			clear: function() {
				let that = this;
				uni.getSystemInfo({
					success: function(res) {
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						that.ctx.clearRect(0, 0, canvasw, canvash);
						that.ctx.draw(true);
					},
				})
			},

			//完成绘画并保存到本地
			finish: function() {
				uni.canvasToTempFilePath({
					canvasId: 'mycanvas',
					success: function(res) {
						let path = res.tempFilePath;
						uni.saveImageToPhotosAlbum({
							filePath: path,
						})
					}
				})
			}
		},
	}
</script>


<style lang="scss" scoped>
	.lucky {
		width: 100%;
		height: 100vh;
		background: url(../../../../static/image/draw-result-bg.png);
		background-size: 100% 100%;
		overflow: hidden;
		// padding-top: 201rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;

		.name {
			width: 373rpx;
			height: 67rpx;
			display: block;
			margin: 201rpx auto 71rpx;
		}

		.content {
			flex: 1;
			display: flex;
			align-items: center;

			.animate {
				width: 560rpx;
				height: 560rpx;
				// position: absolute;
				// top: 50%;
				// left: 50%;
				// transform: translate(-50%,-50%);
				display: block;
				margin: 0 auto;
				/* #ifdef APP-PLUS */
				background: url(../../../../static/image/lucky.webp);
				/* #endif */
				/* #ifdef H5 */
				background: url(../../../../static/image/lucky.png);
				/* #endif */
				background-size: 100% 100%;

				&.active {
					width: 560rpx;
					height: 560rpx;
					background: url(../../../../static/image/draw-bg.png);
					background-size: 100% 100%;
				}
			}

		}

		.mycanvas {
			position: fixed;
			z-index: 100;
			width: 100%;
			height: 100%;
		}

		.bottom {
			width: 100%;
			height: 435rpx;
			// position: fixed;
			// bottom: 0;
		}
	}
</style>
